package com.esproject.Controller;

import com.esproject.bean.ResultItermBean;
import com.esproject.esutils.PageUtil;
import com.esproject.esutils.SearchUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.util.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yan_li on 2017/2/25.
 */
@Controller
@RequestMapping("/search")
public class SearchController extends BaseAction {

    static Map<String, String> parames = Maps.newHashMap();
    static Map<String, String> paramesConver = Maps.newHashMap();

    static {
        parames.put("0.0-24.0", "1-2年");
        parames.put("24.0-48.0", "2-4年");
        parames.put("48.0-10000.0", "4年以上");

        paramesConver.put("1-2年", "0.0-24.0");
        paramesConver.put("2-4年", "24.0-48.0");
        paramesConver.put("4年以上", "48.0-10000.0");
    }

    private String indexName = "cv_info";
    private String typeName = "cv_data";

    @RequestMapping("/index")
    public ModelAndView myclass() {
        System.out.println("ddd");
        return new ModelAndView("/search/index");
    }

    /**
     * 高亮基本的查询方式
     *
     * @param model
     * @param keyWords
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    public ModelAndView getSearchResult(Model model, @RequestParam(value = "keyWords", required = false) String keyWords,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            map = SearchUtil.Highlighter(indexName, typeName, null, "expJob", keyWords, pageSize, (pageNum - 1) * pageSize);
            count = Integer.parseInt(((Long) map.get("total")).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageUtil<Map<String, Object>> page = new PageUtil<Map<String, Object>>(String.valueOf(pageNum), String.valueOf(pageSize), count);
        List<Map<String, Object>> cvList = (List<Map<String, Object>>) map.get("value");
        page.setList(cvList);
        model.addAttribute("total", count);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("page", page);
        model.addAttribute("kw", keyWords);
        return new ModelAndView("/search/index");
    }

    /**
     * 高亮基本的查询方式
     *
     * @param model
     * @param keyWords
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/fact", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8")
    public ModelAndView getFactSearchResult(Model model, @RequestParam(value = "keyWords", required = false) String keyWords,
                                            @RequestParam(value = "edu", required = false) String edu,
                                            @RequestParam(value = "months", required = false) String months,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            Map<String, String> searchFieldmaps = Maps.newHashMap();
            ;
            if (StringUtils.isNotBlank(edu)) {
                searchFieldmaps.put("edu", edu);
            }
            if (StringUtils.isNotBlank(months)) {
                searchFieldmaps.put("workTime", paramesConver.get(months));
            }
            if (pageNum <= 0) {
                pageNum = 1;
            }
            map = SearchUtil.Highlighter(indexName, typeName, searchFieldmaps, "expJob", keyWords, pageSize, (pageNum - 1) * pageSize);
            count = Integer.parseInt(((Long) map.get("total")).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ResultItermBean> eduLists = SearchUtil.ternFact(indexName, typeName, "edu", "expJob", keyWords);
        Map<String, String> searchFieldmaps = Maps.newHashMap();
        if (StringUtils.isBlank(edu)&& eduLists!=null&&eduLists.size()>0&&eduLists.get(0)!=null) {
            searchFieldmaps.put("edu", eduLists.get(0).getName());
        } else {
            searchFieldmaps.put("edu", edu);
        }
        List<ResultItermBean> yearLists = SearchUtil.rangFact(indexName, typeName, searchFieldmaps, "expJob", keyWords);
        List<ResultItermBean> yearLastLists= Lists.newArrayList();
        for(ResultItermBean bean:yearLists){
            bean.setName(parames.get(bean.getName()));
            yearLastLists.add(bean);
        }
        PageUtil<Map<String, Object>> page = new PageUtil<Map<String, Object>>(String.valueOf(pageNum), String.valueOf(pageSize), count);
        List<Map<String, Object>> cvList = (List<Map<String, Object>>) map.get("value");
        page.setList(cvList);
        model.addAttribute("total", count);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("page", page);
        model.addAttribute("kw", keyWords);
        model.addAttribute("edu", eduLists);
        model.addAttribute("year", yearLastLists);
        model.addAttribute("eduClieck", edu);
        model.addAttribute("months", months);
        return new ModelAndView("/search/index");
    }

}
